extends Control

var UnitList = {}
var UnitsTypes = [
	"AA", 
	"Infantry", 
	"Artillery",
	"Armored",
	"Mortar",
	"Reco",
	"Transport"
	]

onready var cam := $HBoxContainer/VBoxContainer/ViewportContainer/Viewport/World/Camera

func reload_units_from_disk():
	for t in UnitsTypes:
		UnitList[t] = get_units_type(t)

func _ready():
	cam.make_current()
	cam.zoom = Vector2(0.2, 0.2)
	reload_units_from_disk()

func _on_AATextureButton_toggled(button_pressed):
	button_pressed and reload_unit_list("AA")


func _on_InfantryTextureButton_toggled(button_pressed):
	button_pressed and reload_unit_list("Infantry")


func _on_ArtilleryTextureButton_toggled(button_pressed):
	button_pressed and reload_unit_list("Artillery")


func _on_ArmoredTextureButton_toggled(button_pressed):
	button_pressed and reload_unit_list("Armored")

func reload_unit_list(type: String):
	$HBoxContainer/UnitList.clear()
	$HBoxContainer/UnitList.fs_path = "res://Units/" + type
	var items : Array = UnitList[type]
	for u in items:
		$HBoxContainer/UnitList.add_item(u)
	
func get_units_type(type: String) -> Array:
	var dir := Directory.new()
	var arr := []
	var path := "res://Units/" + type
	if dir.open(path) == OK:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while file_name != "":
			if file_name != "." or file_name != "..":
				arr.append(file_name.split(".")[0])
				file_name = dir.get_next()
	return arr

